//
//  ForgotPassCon.m
//  ShopDemo
//
//  Created by Sunshine on 2018/9/4.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "ForgotPassCon.h"

@interface ForgotPassCon ()<UITextFieldDelegate>


@property(nonatomic,strong)UITextField *phfield;
@property(nonatomic,strong)UITextField *yzmfield;
@property(nonatomic,strong)UITextField *psfield;
@property(nonatomic,strong)UITextField *cfpsfield;
@property(nonatomic,strong)NSString *yzmStr;

@end

@implementation ForgotPassCon

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"忘记密码";
    [self creatHeaderViewMethod];
    
}

-(void)creatHeaderViewMethod{
    self.view.backgroundColor = LineBackColor;
    UIView *backview = [UIView new];
    backview.backgroundColor = [UIColor whiteColor];
    backview.layer.masksToBounds = YES;
    backview.layer.cornerRadius = 5*kJLXWidthScale;
    [self.view addSubview:backview];
    backview.sd_layout
    .topSpaceToView(self.view, 20*kJLXWidthScale)
    .leftSpaceToView(self.view, 20*kJLXWidthScale)
    .rightSpaceToView(self.view, 20*kJLXWidthScale)
    .heightIs(200*kJLXWidthScale);
    for (int i = 0; i < 4; i ++) {
        UITextField *field = [UITextField new];
        field.delegate = self;
        field.font = [UIFont systemFontOfSize:16*kJLXWidthScale];
        [backview addSubview:field];
        field.sd_layout
        .topSpaceToView(backview, i*50*kJLXWidthScale)
        .rightSpaceToView(backview, 10*kJLXWidthScale)
        .leftSpaceToView(backview, 10*kJLXWidthScale)
        .heightIs(50*kJLXWidthScale);
        if (i == 0) {
            field.placeholder = @"手机号";
            self.phfield = field;
        }else if(i == 1){
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.backgroundColor = [UIColor lightGrayColor];
            btn.layer.masksToBounds = YES;
            btn.layer.cornerRadius = 10*kJLXWidthScale;
            
            [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(yzmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            btn.titleLabel.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [backview addSubview:btn];
            btn.sd_layout
            .topSpaceToView(backview, i*55*kJLXWidthScale)
            .heightIs(40*kJLXWidthScale)
            .rightSpaceToView(backview, 20*kJLXWidthScale)
            .widthIs(110*kJLXWidthScale);
            field.placeholder = @"输入验证码";
            field.sd_layout
            .topSpaceToView(backview, 50*kJLXWidthScale)
            .heightIs(50*kJLXWidthScale)
            .rightSpaceToView(btn, 10*kJLXWidthScale)
            .leftSpaceToView(backview, 10*kJLXWidthScale);
            self.yzmfield = field;
        }else if (i == 2){
            field.secureTextEntry = YES;
            field.placeholder = @"密码";
            self.psfield = field;
        }else if (i == 3){
            field.secureTextEntry = YES;
            field.placeholder = @"确认密码";
            self.cfpsfield = field;
        }
        
        
    }
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 8*kJLXWidthScale;
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitle:@"提 交" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(registerAndLogButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = [UIFont systemFontOfSize:18*kJLXWidthScale];
    [self.view addSubview:btn];
    btn.sd_layout
    .topSpaceToView(backview,80*kJLXWidthScale)
    .centerXEqualToView(self.view)
    .heightIs(45*kJLXWidthScale)
    .leftSpaceToView(self.view, 20*kJLXWidthScale)
    .rightSpaceToView(self.view, 20*kJLXWidthScale);
}

-(void)yzmButtonAction:(UIButton *)sender{
    if (self.phfield.text.length == 0) {
        [self alerWithMessage:@"手机号不能为空"];
    }else{
        if(![MethodClass utilityCheckPhoneNumInput:self.phfield.text]) {
            [self alerWithMessage:@"输入正确的手机号"];
            self.phfield.text = @"";
        }else{
            NSString *urlstr = [NSString stringWithFormat:@"/dashop/sms/app/sendsms.do?PHONE=%@",self.phfield.text];
            NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:urlstr];
            [self YZMMethodWithUrl:endstr];
            NSInteger num = 60;
            [MethodClass verificationCode:num blockYes:^{
                [sender setTitle:@"获取验证码" forState:UIControlStateNormal];
            } blockNo:^(id time) {
                [sender setTitle:time forState:UIControlStateNormal];
            }];
            
        }
    }
    
    
    
}

-(void)YZMMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSDictionary *dic = response;
        self.yzmStr = dic[@"STATUSCODE"];
    } failBlock:^(NSError *error) {
        
    }];
}



-(void)registerAndLogButtonAction:(UIButton *)sender{
    [self.view endEditing:YES];
    if (self.phfield.text.length == 0) {
        [self alerWithMessage:@"输入正确的手机号"];
    }else if (self.psfield.text.length == 0){
        [self alerWithMessage:@"输入密码"];
    }else if(self.yzmfield.text.length == 0){
        [self alerWithMessage:@"验证码不能为空"];
    }else if(self.cfpsfield.text == 0){
        [self alerWithMessage:@"确认密码不能为空"];
    }else{
        /*
         1.1  忘记密码后修改密码
         接口参数 PASSWORD 密码
         PHONE 手机号
         否需要分页 否
         接口地址 happuser/app/forget.do
         返回接口 map
         */
        NSString *strurl = [NSString stringWithFormat:@"/dashop/happuser/app/forget.do?PASSWORD=%@&PHONE=%@",self.cfpsfield.text,self.phfield.text];
        NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
        NSString  *newUrlString = [endstr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [self RegistMethodWithUrl:newUrlString];
    }
}

-(void)RegistMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSDictionary *dic = response;
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            if ([dic[@"RESULT"] integerValue] == 1) {
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [self alerWithMessage:dic[@"MESSAGE"]];
            }
        }];
    } failBlock:^(NSError *error) {
        
    }];
}






//已经取消编辑状态 结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.placeholder isEqualToString:@"输入验证码"]) {
        if (![textField.text isEqualToString:self.yzmStr]) {
            [self alerWithMessage:@"验证码不正确"];
            textField.text = @"";
        }
    }else if ([textField.placeholder isEqualToString:@"确认密码"]){
        if (![textField.text isEqualToString:self.psfield.text]) {
            [self alerWithMessage:@"密码不一致"];
            textField.text = @"";
        }
        
    }
    
}
//封装提示框方法
-(void)alerWithMessage:(NSString *)msg{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:^{
        //延迟1秒自动消失
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [alert dismissViewControllerAnimated:YES completion:nil];
        });
    }];
}


@end
